﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DIS.Infrastructure.Entities.Entity;
using DIS.Portal.WebLogic;
using DIS.Infrastructure.Entities.Enum;
using DIS.Portal.Biz;

namespace IDS.Portal.Web.Controllers
{
    [Auth]
    public class SystemsetController : Controller
    {
        //
        // GET: /Systemset/

        public ActionResult Index()
        {
            return View();
        }
        #region 填报周期

        public ActionResult FinancialPeriod(int page = 0)
        {
            var data = new FinancialPeriod().FindAll(c => 1 == 1, c => c.Year + c.Month, page, 20);
            ViewBag.FinancialPeriods = new ItemListBox<FinancialPeriod>(data).BuildPage(
                   new FinancialPeriod().Count(c => 1 == 1), page, 20, new PageParameter() { Style = "workspace" });
            return View();
        }

        public ActionResult FinancialPeriodDetail(int Id = 0)
        {
            int nowYear = DateTime.Now.Year;
            List<string> yearList = new List<string>();
            for (int i = nowYear - 10; i < nowYear + 10; i++)
            {
                yearList.Add(i.ToString());
            }
            ViewBag.yearList = yearList;
            var fp = new FinancialPeriod().Find(Id.ToString());
            ViewBag.Fp = fp;
            return View();
        }

        [HttpPost]
        public ActionResult SaveFinancialPeriod(string year, string month, string startdate, string enddate, int Id = 0)
        {

            if (Id > 0)
            {
                var fpTemp = new FinancialPeriod().Find(Id.ToString());
                fpTemp.StartDate = startdate;
                fpTemp.Month = month;
                fpTemp.EndDate = enddate;
                fpTemp.Year = year;
                fpTemp.PostModify();
            }
            else
            {
                var fp = new FinancialPeriod()
                {
                    EndDate = enddate,
                    Month = month,
                    StartDate = startdate,
                    Year = year
                };
                fp.PostAdd();
            }
            return Redirect("/Systemset/FinancialPeriod");
        }

        #endregion
        #region 项目
        public ActionResult Projects(int page = 0)
        {
            var data = new Project().FindAll(c => 1 == 1, c => c.ProjectCode, page, 20);
            ViewBag.Projects = new ItemListBox<Project>(data).BuildPage(
                   new Project().Count(c => 1 == 1), page, 20, new PageParameter() { Style = "workspace" });
            return View();
        }
        public ActionResult ProjectDetail(int Id = 0)
        {
            var project = new Project().Find(Id.ToString());
            ViewBag.project = project;
            return View();
        }
        [HttpPost]
        public ActionResult SaveProject(string projectCode, string projectName, int Id = 0)
        {
            if (Id > 0)
            {
                var project = new Project().Find(Id.ToString());
                project.ProjectCode = projectCode;
                project.ProjectName = projectName;
                project.PostModify();
            }
            else
            {
                var project = new Project()
                {
                    ProjectCode = projectCode,
                    ProjectName = projectName,
                    IsDeleted = 0
                };
                project.PostAdd();
            }
            return Redirect("/Systemset/Projects");
        }
        #endregion

        #region 补助设置
        public ActionResult SetAllowanceMoney()
        { 
            string allowanceMoney = SupportBiz.GetAllowanceMoney().ToString();
            ViewBag.allowanceMoney = allowanceMoney;

            return View();
        }
        [HttpPost]
        public ActionResult SetAllowanceMoney(string money)
        {
            SupportBiz.WriteAllowanceMoney(money);
            return Redirect("/Systemset/SetAllowanceMoney");
        }
        #endregion

    }
}
